// Package gen
// @Author: zhangdi
// @File: func_form
// @Version: 1.0.0
// @Date: 2023/9/6 18:02
package gen

import (
	"fmt"
	"server/application/constant/enums"
	"server/pkg/utils"
	"strings"
)

var uploadFormTypes = []string{enums.FormTypeUpload, enums.FormTypeUploadImage,
	enums.FormTypeUploads, enums.FormTypeUploadImages}

var dicFormTypes = []string{enums.FormTypeSelect, enums.FormTypeSwitch,
	enums.FormTypeRadio}

const importUploadStr = `import { defaultUploadProps } from "@/components/FormCreate/defaultProps/upload";`
const importOptionsStr = `import { getDicOptions } from "@/components/FormCreate/request";`

// GetDefaultFormProps 获取表单的默认props和import语句
func GetDefaultFormProps(formType string) (string, string) {

	switch formType {
	case enums.FormTypeUpload: //单文件上传
		return "defaultUploadProps('file',false)", importUploadStr
	case enums.FormTypeUploads: //多文件上传
		return "defaultUploadProps('file',true)", importUploadStr
	case enums.FormTypeUploadImage: //单图上传
		return "defaultUploadProps('image',false)", importUploadStr
	case enums.FormTypeUploadImages: //多图上传
		return "defaultUploadProps('image',true)", importUploadStr
	case enums.FormTypeSelect, enums.FormTypeRadio: //单选 下拉
		return "{}", importOptionsStr
	}
	return `{}`, ``
}

// 获取Effect属性：必填 字典获取操作
func getFormRuleEffect(field *FieldInfo) string {
	res := "{"

	if field.EditConfig.FormType == enums.FormTypeSelect || field.EditConfig.FormType == enums.FormTypeRadio {
		res += fmt.Sprintf(`fetch: getDicOptions("%s"),`, field.EditConfig.DicKey)
	}
	return strings.TrimRight(res, ",") + "}"
}

func GetListColumnsEnumsString(field *FieldInfo) string {
	res := ""
	if FormTypeIsDic(field.EditConfig.FormType) {
		return `,enum: () => requestSysDictionaryGetOptions("` + field.EditConfig.DicKey + `")`
	}
	return res
}
func getFormType(field *FieldInfo) string {
	if field.EditConfig.FormType == "input" {
		if toTsTypeByString(field.TypeGo) == "number" {
			return "number"
		} else {
			return "input"
		}
	}

	if FormTypeIsUpload(field.EditConfig.FormType) {
		return "upload"
	}
	return field.EditConfig.FormType
}

// 是否为上传类型
func FormTypeIsUpload(formType string) bool {
	if utils.ArrContains(uploadFormTypes, formType) {
		return true
	}
	return false
}

// FormTypeIsDic 是否为字典类型
func FormTypeIsDic(formType string) bool {
	if utils.ArrContains(dicFormTypes, formType) {
		return true
	}
	return false
}

func getRequireParam(field *FieldInfo) string {
	//tsType := "string"
	tsTypeMsg := "(字符串类型)"
	if toTsTypeByString(field.TypeGo) == "number" {
		//tsType = "number"
		tsTypeMsg = "(数字类型)"
	}

	//上传组件的验证类型需要为array
	if FormTypeIsUpload(field.EditConfig.FormType) {
		//tsType = "array"
	}

	//字典类型,默认都是string
	if FormTypeIsDic(field.EditConfig.FormType) {
		//tsType = "string"
		tsTypeMsg = ""
	}

	res := "["
	if field.EditConfig.FormRequired {
		//type:"string", 删掉了类型
		res += `{required: true, message: "必填项：` + field.Label + tsTypeMsg + `" },`
	}
	return strings.TrimRight(res, ",") + "]"
}
